Ext.namespace("Plan.Prepare");

Plan.Prepare.panel = function(){
	Plan.Add.win.instance = new Plan.Add.win();
	Plan.Add.win.instance.initData();
	
	this.store = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({
			url: '../plan/searchPlanInfo.form?timesnap=' + Date.parse(new Date()),
			method: 'POST'
		}),
		reader: new Ext.data.JsonReader({
			root: 'result',
			totalProperty: 'total',
			id: 'PLAN_ID',
			fields: ['PLAN_ID', 'PLAN_TITLE', 'CREATE_NAME','CREATE_DATE', 'PLAN_TYPE_NAME', 'AUDIT_ID','PLAN_START_DATE',
			'PLAN_END_DATE','PLAN_STATUS','PLAN_FINISH_STATUS','DEPARTMENT','PLAN_LEVEL','PLAN_TYPE_ID','AUDIT_NAME']
		}),listeners:{
			 beforeload :{    
	    	  	fn : function(thiz,options){
       	          Ext.apply(thiz.baseParams,
       	          { 
       	          	plan_status:[4,2]
       	          });    
              },
              scope:this 
	    	}
		}
		
	});
	this.renderStatus = function(value){
		return OMS.PLANSTATUS[value];
	};
	this.renderLevel = function(value){
		return OMS.PLAN_LEVEL[value];
	};
	this.renderDays = function(value){
		if(!value)
			return;
		var date = Date.parseDate(value.substring(0,10),"Y-m-d");
		var days = parseInt((date.getTime()-new Date().getTime())/(1000*60*60*24));
		if(days<0)
			return "<font color='red' size=1>逾期"+(-days)+"天</font>";
		else
			return "<font size=1>还剩"+days+"天</font>";
	};
	this.sm =  new Ext.grid.CheckboxSelectionModel();
	this.cm = new Ext.grid.ColumnModel({
	    defaults: {
	        width: 100,
	        sortable: true,
	        align: 'center'
	    },
	    columns: [
	    	this.sm,
			{id: 'PLAN_ID', header: 'ID', width: 50, hidden: true, dataIndex: 'PLAN_ID'},
			{header: '计划名称', width: 200, dataIndex: 'PLAN_TITLE'},
			{header: '计划类型', width: 100, dataIndex: 'PLAN_TYPE_NAME'},
			{header: '制定人', width: 100, dataIndex: 'CREATE_NAME'},
			{header: '审核人', dataIndex: 'AUDIT_NAME'},
			{header: '开始时间', dataIndex: 'PLAN_START_DATE'},
			{header: '结束时间', dataIndex: 'PLAN_END_DATE'},
		//	{header: '剩余天数', dataIndex: 'PLAN_END_DATE',renderer:this.renderDays},
			{header: '制定部门', dataIndex: 'DEPARTMENT'},
			{header: '计划等级', dataIndex: 'PLAN_LEVEL',renderer:this.renderLevel},
			{header: '计划状态', dataIndex: 'PLAN_STATUS',renderer:this.renderStatus}
	    ]
	});
	this.btnAdd = new Ext.Button({
		text:'新建计划' ,
		scope:this,
		handler:this.showAdd
	});
	this.btnSend = new Ext.Button({
		text:'上报计划' ,
		scope:this,
		handler:this.sendPlan
	});
	this.grid = new Ext.grid.GridPanel({
		region:'center',
		store:this.store,
		sm:this.sm,
		cm:this.cm,
        loadMask: {msg: '加载中，请稍候...'},
        tbar:[this.btnAdd,'-',this.btnSend],
        bbar:new Ext.PagingToolbar({
			 pageSize: 15,
	         store: this.store,
	         displayInfo: true,
	         displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
	         emptyMsg: "没有记录..."
		}),
        viewConfig: { forceFit: true },
        listeners:{
      		  rowdblclick : function( grid, rowIndex,e){ 
					Plan.Add.win.instance.initUpdate(grid.store.getAt(rowIndex).data);
					Plan.Add.win.instance.show();
			}
       }
	});
	Plan.Prepare.panel.superclass.constructor.call(this,{
		title:'草稿箱',
		layout:'border',
		items:[this.grid],
		listeners:{
			render:this.queryPlan
		}
	});
}
Ext.extend(Plan.Prepare.panel,Ext.Panel,{
	showAdd:function(){
		if(!Plan.Add.win.instance)
			Plan.Add.win.instance = new Plan.Add.win();
		Plan.Add.win.instance.show();
		Plan.Add.win.instance.initAdd();
	},
	queryPlan:function(){
		this.store.load({
			params:{
				start : 0,
				limit : 15,
				plan_status:[4,2]
				}
		})
	},
	sendPlan:function(){
		var sm =this.grid.getSelectionModel();
		var record = sm.getSelections();
		if(record.length==0){
			Utils.warn('请选择一条计划！');
			return ;
		}
		Ext.Msg.confirm("信息","确认上报?",function(btn){
			if(btn=="yes"){
				var planId =[];
				for(var i=0;i<record.length;i++){
					planId.push(record[i].data.PLAN_ID)
				}
				//上报url
				var conn = new Ext.data.Connection({
					url:'../plan/uptPlanStatus.form',
					method:'post'
				})
				Utils.request(conn,{params:{
						planId:planId,
						plan_status:'0'
					},success:function(){
						Utils.info("操作成功！");
						Plan.Prepare.panel.instance.store.reload();
						Plan.Audit.panel.instance.store.reload();
					}
				})
			}
		},this);
	}
});
